#ifndef WATCH_DOG_H
#define WATCH_DOG_H

#include "object/launch_node.h"
#include <unordered_map>

#define pWatchDog WatchDog::getWatchDog()

class WatchDog
{
public:
    static WatchDog *getWatchDog();

    /**
     * @brief:  创建节点
     */
    void createNode(const LaunchNode &launchNode);
    void createNode(const NodeInfo &nodeInfo);

    /**
     * @brief:  重新创建节点，用于节点退出后使用
     */
    void recreateNode(const NodeInfo &nodeInfo);

    /**
     * @brief:  监控进程状态,阻塞式样
     * @param[in] millSec 监控时间间隔，单位：ms
     */
    void startWatchWork(int millSec);

private:
    WatchDog();

    void savePid(const NodeInfo &nodeInfo, pid_t pid);
    void removePid(const NodeInfo &nodeInfo);

    /**
     * @brief:  监控某一个节点
     */
    void watchNode(const NodeInfo &nodeInfo, pid_t pid);
    void watchNode();

    static std::string generateKey(const NodeInfo &nodeInfo);

private:
    std::unordered_map<std::string, pid_t> pidMap;//pkg-type-name: pid
    std::unordered_map<std::string, NodeInfo> nodeInfoMap;//pkg-type-name: node info
};


#endif //WATCH_DOG_H
